<?php
/*
Boite à outils de manipulation de variables

file version 2005-04-20 10:26:12
*/

// fusionne deux tableaux recursivement, avec remplacement des collisions
function & array_merge_recursive_replace(&$array_1,&$array_2) {
	$merged=$array_1;
	$keys_1=array_keys($array_1);
	$keys_2=array_keys($array_2);
	foreach ($merged as $key => $value) {
		if (!isset($array_2[$key])) continue;
		if (is_array($value) and is_array($array_2[$key])) {
			// appel recursif
			$merged[$key]=array_merge_recursive_replace($value,$array_2[$key]);
		} else {
			$merged[$key]=$array_2[$key];
		}
	}
	foreach ($array_2 as $key => $value)
		if (!isset($merged[$key])) $merged[$key]=$value;
	return $merged;
}

// retourne la declaration php d'une variable (recursif)
// si key est nul, seule la partie valeur est retournée
function varToDeclaration($var,$key=NULL) {
	switch (strtolower(gettype($var))) {
	case 'boolean':
	case 'bool':
		if ($var) $r='true';
		else $r='false';
		break;
	case 'null':
		$r='NULL';
		break;
	case 'string':
		$r="'".str_replace("'","\'",$var)."'";
		break;
	case 'int':
	case 'double':
	case 'decimal':
	case 'integer':
	case 'float':
		$r=$var;
		break;
	case 'array':
		$r="array(\n";
		foreach ($var as $key2 => $value) {
			$r.="'$key2'=>".varToDeclaration($value).",";
		}
		// suppression derniere virgule superflue
		if (substr($r,-1,1)==',') $r=substr($r,0,-1);
		$r.=")";
		break;
	default:
		//echo gettype($var).':'.$var;
		$r='NULL';
		break;
	}
	if ($key!==NULL) return $key."=".$r.";";
	else return $r;
}

function getGPCVar($space,$key) {
	$space=strtolower($space);
	switch ($space) {
	case 'post':
		if (!isset($_POST[$key])) return NULL;
		$value=$_POST[$key];
		break;
	case 'get':
		if (!isset($_GET[$key])) return NULL;
		$value=$_GET[$key];
		break;
		case 'cookie':
		if (!isset($_COOKIE[$key])) return NULL;
		$value=$_COOKIE[$key];
		break;
	default:
		return NULL;
		break;
	}
	// ajout de slash si nécessaire
	if ($this->parameters['slashGPC'] and !get_magic_quotes_gpc())
		$value=addslashes($value);
	// enlevement de slashes si nécessaire
	else if (!$this->parameters['slashGPC'] and get_magic_quotes_gpc())
		$value=stripslashes($value);
	return $value;
}
?>
